"""
Phase 4: Housing Markets
=========================
Z → house prices. BIS coverage ~40-60 countries, mostly OECD.
"""

import sys
from pathlib import Path

import numpy as np
import pandas as pd

PROJECT_DIR = Path("/mnt/c/demographics_capital_flows/asset_returns")
MULTILATERAL_DIR = PROJECT_DIR.parent / "multilateral"
sys.path.insert(0, str(MULTILATERAL_DIR / "src"))
from model import PanelGLS

PROCESSED_DIR = PROJECT_DIR / "data" / "processed"
TABLES_DIR = PROJECT_DIR / "output" / "tables"

OECD_38 = [
    "AUS", "AUT", "BEL", "CAN", "CHL", "COL", "CRI", "CZE", "DNK", "EST",
    "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL", "ISR", "ITA", "JPN",
    "KOR", "LVA", "LTU", "LUX", "MEX", "NLD", "NZL", "NOR", "POL", "PRT",
    "SVK", "SVN", "ESP", "SWE", "CHE", "TUR", "GBR", "USA",
]

CONTROLS = ['rgdp_growth', 'real_bond_10y', 'inflation', 'kaopen']


def stars(p):
    if p < 0.01: return '***'
    if p < 0.05: return '**'
    if p < 0.10: return '*'
    return ''


def run_model(df, dep_var, regressors, label):
    regressors = [r for r in regressors if r in df.columns]
    if dep_var not in df.columns:
        print(f"  [{label}] {dep_var} missing — skipping")
        return None

    sub = df.dropna(subset=[dep_var] + regressors).copy()
    if len(sub) < 50:
        print(f"  [{label}] Insufficient obs ({len(sub)}) — skipping")
        return None

    gls = PanelGLS()
    gls.fit(sub[dep_var].values, sub[regressors].values,
            sub['iso3'].values, sub['year'].values)

    print(f"\n  [{label}]  N={gls.n_obs}, countries={gls.n_countries}, "
          f"R²={gls.r_squared:.4f}")

    results = {
        'label': label, 'dep_var': dep_var,
        'n_obs': gls.n_obs, 'n_countries': gls.n_countries,
        'r_squared': gls.r_squared, 'rho': gls.rho,
    }
    for i, name in enumerate(regressors):
        results[f'coef_{name}'] = gls.beta[i]
        results[f'se_{name}'] = gls.se[i]
        results[f'p_{name}'] = gls.pvalues[i]
        sig = stars(gls.pvalues[i])
        print(f"    {name:<25} {gls.beta[i]:>8.4f} ({gls.se[i]:.4f}) {sig}")

    return results


def main():
    print("=" * 70)
    print("PHASE 4: Housing Markets")
    print("=" * 70)

    df = pd.read_csv(PROCESSED_DIR / "asset_panel.csv")
    print(f"Panel: {df['iso3'].nunique()} countries, {len(df)} obs")

    # Check HPI coverage
    if 'd_rhpi' in df.columns:
        hpi_countries = df.loc[df['d_rhpi'].notna(), 'iso3'].nunique()
        hpi_obs = df['d_rhpi'].notna().sum()
        print(f"HPI coverage: {hpi_countries} countries, {hpi_obs} obs")
    else:
        print("WARNING: d_rhpi not in panel — BIS download may have failed")

    all_results = []
    demo_vars = ['Z_1', 'Z_2', 'Z_3']
    controls = [c for c in CONTROLS if c in df.columns]

    # ── Model 1: d_rhpi = Z + controls ──
    r = run_model(df, 'd_rhpi', demo_vars + controls, "M1: Z → Δreal HPI")
    if r: all_results.append(r)

    # ── Model 2: old_dep + youth_dep ──
    age_vars = ['old_dep', 'youth_dep']
    r = run_model(df, 'd_rhpi', age_vars + controls, "M2: age ratios → Δreal HPI")
    if r: all_results.append(r)

    # ── Model 3: Z × KAOPEN ──
    int_vars = [v for v in ['Z_1_x_kaopen', 'Z_2_x_kaopen', 'Z_3_x_kaopen']
                if v in df.columns]
    if int_vars:
        r = run_model(df, 'd_rhpi', demo_vars + controls + int_vars,
                      "M3: Z×KAOPEN → Δreal HPI")
        if r: all_results.append(r)

    # ── Model 4: CA surplus → housing ──
    if 'ca_gdp' in df.columns:
        r = run_model(df, 'd_rhpi', ['ca_gdp'] + controls, "M4: CA/GDP → Δreal HPI")
        if r: all_results.append(r)

    # ── Model 5: OECD subsample ──
    oecd = df[df['iso3'].isin(OECD_38)].copy()
    r = run_model(oecd, 'd_rhpi', demo_vars + controls, "M5: OECD Z → Δreal HPI")
    if r: all_results.append(r)

    # ── Model 6: LE² interaction ──
    if 'life_expectancy' in df.columns and 'life_expectancy_sq' in df.columns:
        le_vars = ['life_expectancy', 'life_expectancy_sq']
        r = run_model(df, 'd_rhpi', demo_vars + controls + le_vars,
                      "M6: Z + LE² → Δreal HPI")
        if r: all_results.append(r)

    # ── Model 7: Post-GFC (2010+) ──
    post_gfc = df[df['year'] >= 2010].copy()
    r = run_model(post_gfc, 'd_rhpi', demo_vars + controls, "M7: post-GFC Z → Δreal HPI")
    if r: all_results.append(r)

    # ── Build table ──
    build_table(all_results)

    print("\n" + "=" * 70)
    print("Phase 4 complete.")
    print("=" * 70)


def build_table(all_results):
    if not all_results:
        return

    key_vars = ['Z_1', 'Z_2', 'Z_3', 'old_dep', 'youth_dep', 'ca_gdp',
                'life_expectancy', 'life_expectancy_sq',
                'Z_1_x_kaopen', 'Z_2_x_kaopen', 'Z_3_x_kaopen']

    md = ["# Housing Market Results\n"]
    md.append("| Model | Dep Var | N | Countries | R² |")
    md.append("|---|---|---|---|---|")
    for r in all_results:
        md.append(f"| {r['label']} | {r['dep_var']} | {r['n_obs']} "
                  f"| {r['n_countries']} | {r['r_squared']:.3f} |")

    md.append("\n## Key Coefficients\n")
    md.append("| Model | Variable | Coef | SE | p-value | Sig |")
    md.append("|---|---|---|---|---|---|")
    for r in all_results:
        for var in key_vars:
            ckey = f'coef_{var}'
            if ckey in r:
                p = r[f'p_{var}']
                md.append(f"| {r['label']} | {var} | {r[ckey]:.4f} "
                          f"| {r[f'se_{var}']:.4f} | {p:.4f} | {stars(p)} |")

    md.append(f"\n*Controls: {', '.join(CONTROLS)}*")

    out = TABLES_DIR / "housing.md"
    out.write_text('\n'.join(md))
    print(f"  Saved: {out}")


if __name__ == "__main__":
    main()
